/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.sistemaacademico;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import br.edu.sistemaacademico.facade.DataFacade;
import br.edu.sistemaacademico.pojo.Disciplina;
import br.edu.sistemaacademico.pojo.Professor;
import br.edu.sistemaacademico.state.StateHandler;
import client.ClientCommand;

public class frmProfessorServidor extends FrmModelo{
	Professor professor;
	StateHandler stateAlterarExcuir = new StateHandler(false);
	public List<Object> campos = ClientCommand.getCampos("ProfessorService"); //new ArrayList<Object>();
    private List<Object> valores = new ArrayList<Object>();
    private List<Object> tamanho = new ArrayList<Object>();
    List<Professor> extra;
    TabelaModelo tabModel;

    public frmProfessorServidor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        tamanho.add("70");
        tabModel = new TabelaModelo(campos, tamanho, getJTable());
        tabModel.busca(getTfBuscar());
        ListSelectionModel listModel = getJTable().getSelectionModel();
        listModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        listModel.addListSelectionListener(list);
        
        setTitle("Professor");
        setButtons();

        getbtnVoltar().addActionListener(listener);
        getbtnVoltar().setActionCommand("voltar");
        
        getbtnExcluir().addActionListener(listener);
        getbtnExcluir().setActionCommand("excluir");
        
        getbtnAlterar().addActionListener(listener);
        getbtnAlterar().setActionCommand("alterar");
        getJbtnGravar().setActionCommand("gravar");
        getJbtnGravar().addActionListener(listener);
        setStateAlteraExcuir();
        atualiza();
        setVisible(true);
    }
    
        public void setButtons(){
        JButton btn1 = getJbtnGravar();
        JButton btn2 = getbtnAlterar();
        JButton btn3 = getbtnExcluir();
        JButton btn4 = getbtn4();
        JButton btn5 = getbtn5();
        JButton btn6 = getbtn6();
        
        btn1.setText("Novo");
        btn3.setText("Excluir");
        btn4.setText("Relatorios");

        btn5.setVisible(false);
        btn6.setVisible(false);
    }
        public void atualiza(){
        	((DefaultTableModel) getJTable().getModel()).setNumRows(0);
			getJTable().updateUI();
        	this.extra = (List)ClientCommand.carregar("ProfessorService","codigo = codigo", "");
    		DataFacade entrada = new DataFacade();   
        	for(int i = 0; i < extra.size();i++ ){
        		valores = new LinkedList<Object>();
        		valores.add(extra.get(i).getId());
        		valores.add(extra.get(i).getNome());
        		valores.add(extra.get(i).getCpf());
        		valores.add(extra.get(i).getEndereco());
        		valores.add(extra.get(i).getTelefone());
        		valores.add(extra.get(i).getSexo());
        		valores.add(extra.get(i).getRg());
        		valores.add(extra.get(i).getCtps());
        		valores.add(extra.get(i).getGraducao());
        		valores.add(extra.get(i).getEspecializacao());
        		valores.add(extra.get(i).getMestrado());
        		valores.add(extra.get(i).getDoutorado());     		
        		valores.add(entrada.getString(extra.get(i).getAnoEntrada()));
        		valores.add(entrada.getString(extra.get(i).getAnosaida()));
        		valores.add(extra.get(i).getLogin());
        		//valores.add(extra.get(i).getSenha());
        		valores.add("********");
        		tabModel.setValores(valores);
        		valores.clear();       		
        	}
        }
        ActionListener listener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getActionCommand().equals("voltar")){
				setVisible(false);
					return;
				}else if(e.getActionCommand().equals("alterar"))
					new frmAtualizarProfessor(professor);
				else if(e.getActionCommand().equals("excluir"))
					remover();
				else if(e.getActionCommand().equals("gravar"))
					new frmCadastroProfessor();
				atualiza();
			}
		};
		
		ListSelectionListener list = new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				try{
					professor = getSelectedProfessor(Long.parseLong(getJTable().getValueAt(getJTable().getSelectedRow(), 0).toString()));
					stateAlterarExcuir.change(true);
				}catch(ArrayIndexOutOfBoundsException ea){
					professor = null;
					stateAlterarExcuir.change(false);
				}	
			}
		};	
		
		public Professor getSelectedProfessor(Long codigo){
			return (Professor) ClientCommand.carregaObjeto("ProfessorService", "codigo ="+codigo, "");
		}
		public void remover(){
			ClientCommand.deletar("ProfessorService",professor);
		}
	    public void setStateAlteraExcuir(){
	    	stateAlterarExcuir.addComponent(getbtnAlterar());
	    	stateAlterarExcuir.addComponent(getbtnExcluir());
	    	stateAlterarExcuir.addComponent(getbtn4());
	    	stateAlterarExcuir.addComponent(getbtn5());
	    }
}
